package Element;

import Data.Data;
import Illness.Illness;

//element of the grid
//it will fill the grid and change day by day 

public class Element {

    private Illness illness;

    private String name;
    private boolean isAlive = true;
    private int sickTime;// time during the element get the illness
    private int nSickTime = 0;// number of time the element recover from an
                              // illness,it influence the resistance against
                              // illness

    private int humanDeadRate = 10;
    private int[] elementContamination = new int[Data.nClassElement];// ={humanContamination,chickenContamination,duckContamination,pigContamination};

    private int[] elementContact; // = {humanContact,
                                  // chickenContact,duckContact, pigContact};
    private final int per = 100; // per cent

    // if there is no element we use that constructor
    public Element() {
        name = Data.nullElementName;
    }

    // if there is no element we use that constructor
    public Element(String s, int elementContact[]) {
        name = s;
        this.elementContact = elementContact;
    }

    // if there is an element
    public Element(String s, int elementContact[], Illness ill) {
        name = s;
        this.elementContact = elementContact;

        setSick(ill);

    }

    // this fonction gives the element current state
    public String getStat() {
        if (this.isSick()) {
            if (sickTime < Data.noContagiousTime) {
                return Data.noContagiousName;
            } else if (sickTime < Data.contagiousTime) {
                return Data.contagiousName;
            } else {
                return Data.recoveryName;
            }
        } else {
            return Data.healthy;
        }

    }

    // test if "this" will infect name2 element
    public boolean infect(String name2) {
        java.util.Random r = new java.util.Random();
        int rand = r.nextInt(per);

        if (name2 == Data.humanName && rand <= this.elementContamination[0]) {
            return true;
        } else if (name2 == Data.chickenName
                && rand <= this.elementContamination[1]) {
            return true;
        } else if (name2 == Data.duckName
                && rand <= this.elementContamination[2]) {
            return true;
        } else if (name2 == Data.pigName
                && rand <= this.elementContamination[3]) {
            return true;
        } else {
            // gap
        }
        return false;
    }

    // this fonction improve sickTime, heals the element if he's recovering time
    // end and test if a human die or recovering
    public void illnessEvolution(boolean isHuman) {
        if (isHuman && sickTime == Data.contagiousTime - 1) {
            java.util.Random r = new java.util.Random();
            int rand = r.nextInt(per);
            if (rand <= this.humanDeadRate) {
                this.isAlive = false;
                this.illness = null;
                sickTime = 0;
            }
        }
        if (sickTime == Data.recoveryTime) {
            this.illness = null;
            sickTime = 0;
            nSickTime++;
        }

        sickTime++;
    }

    // test if the element is contagious
    public boolean isContagious() {
        if (sickTime > Data.noContagiousTime && sickTime <= Data.contagiousTime) {
            return true;
        }
        return false;
    }

    // test if "this" is sick
    public boolean isSick() {
        if (illness == null) {
            return false;
        } else
            return true;
    }

    // complete the information about element when he become sick
    public void setSick(Illness ill) {
        illness = ill;
        sickTime = 1;

        for (int i = 0; i < this.elementContamination.length; i++) {
            elementContamination[i] = ill.getElementCont()[i]
                    * this.elementContact[i] - this.nSickTime
                    * Data.resistanceRate;
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }

    public void setSickTime(int t) {
        sickTime = t;
    }

    public int getSickTime() {
        return sickTime;
    }

    public boolean getIsAlive() {
        return isAlive;
    }

    public Illness getIllness() {
        return illness;
    }

    @Override
    public String toString() {
        return "Element [name=" + name + ", sickTime=" + sickTime
                + ", nSickTime=" + nSickTime + "]";
    }

}
